<template>
	<view>
		<u-navbar title="選擇商品" placeholder autoBack>
		</u-navbar>
		<view class="mainpadding" style="padding-bottom: 150rpx;">
			<view class="mainpadding ffffff radius">
				<view class="flexbetween">
					<view class="titletext bold hei_text">商品</view>
					<view class="huang_text main_size bold flexright" @click="checkservice">
						<view class="margin_right1">增加商品</view>
						<u-icon name="arrow-right" color="#ff8505" size="16"></u-icon>
					</view>
				</view>
				<view :class="index==4?'':'pdl'" v-for="(item,index) in list.data" :key="item.id">
					<view class="flexleft margin_top dingwei">
						<view class="listleftimg radius margin_right">
							<image class="listleftimg radius" :src="item.logoimage" mode=""></image>
						</view>
						<!-- <view class="saleLableclass" v-if="item.salename">{{item.salename}}</view> -->
						<view class="justify_between_column" style="height: 210rpx;width: 100%;">
							<view class="hei_text main_size bold" v-if="item.salename">【{{item.salename}}】</view>
							<view class="hei_text main_size bold">{{item.goods_name}}</view>
							<view class="xiaohui margin_top1 flexbetween" v-if="item.wei || item.hei">
								<text>尺寸：{{item.wei}}*{{item.hei}}</text>
								<text>用量：{{item.formulacount}}{{item.size}}</text>
							</view>
							<view class="xiaolan margin_top1"
								v-if="item.spunatdata_text && item.spunatdata_text.length">
								規格：<text class="margin_right1" v-for="(value) in item.spunatdata_text"
									:key="value.spunatid">{{value.spunatname}}*{{value.num}}&nbsp;&nbsp;&nbsp;</text>
							</view>
							<view class="flexbetween margin_top1">
								<view class="main_size huang_text bold">${{item.d_price}}&nbsp;&nbsp;&nbsp;x{{item.num}}
									<!-- <text class="hong_text">稅金{{item.sj_price}}</text> -->
								</view>
								<view class="xiaohuang main_size huang_text">${{item.price}}</view>

							</view>
							<view class="flexright margin_top1">
								<view class="margin_right flexleft" @click="removeGoods(item.id)">
									<u-icon name="trash" color="#333" size="16"></u-icon>
									<view class="xiaohei">刪除</view>
								</view>
								<view class="flexleft" @click="editGoods(item)">
									<u-icon name="edit-pen" color="#333" size="16"></u-icon>
									<view class="xiaohei">編輯</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="mainpadding ffffff radius">
				<view class="titletext bold hei_text">訂單信息</view>
				<view class="flexbetween margin_top">
					<view class="hei_text main_size">商品總價</view>
					<view class="huang_text main_size" v-text="'$'+goodTotalPrice"></view>
				</view>
				<view class="flexbetween margin_top">
					<view class="hei_text main_size">稅金（5%）</view>
					<input type="number" placeholder="請輸入稅金" class="hei_text main_size textright" disabled=""
						v-model="s_money">
				</view>
			</view>
			<view class="mainpadding2 ffffff line_b9" v-if="appkey!=''&&is_call_sdkfp==0">
				<view class="flexbetween">
					<view class="text_weight color_san white_nowrap">發票類型<text style="color: #ff0000;">*</text></view>
					<u-switch inactiveColor="rgb(230, 230, 230)" v-model="fpopen" activeColor="#ffb800"></u-switch>
				</view>
				<template v-if="fpopen">
					<view class="margin_top flexbetween">
						<view :class="opentype==0?'flexleft select':'flexleft noSelect'" @click="changeOpentype(0)">
							<image class="vote1Img"
								:src="opentype==0?'@/static/images/mine/vote1_active.png':'@/static/images/mine/vote1.png'"
								mode=""></image>
							<view>紙本</view>
						</view>
						<view :class="opentype==1?'flexleft select':'flexleft noSelect'" @click="changeOpentype(1)">
							<image class="vote2Img"
								:src="opentype==1?'@/static/images/mine/vote2_active.png':'@/static/images/mine/vote2.png'"
								mode=""></image>
							<view>載具</view>
						</view>
						<view :class="opentype==2?'flexleft select':'flexleft noSelect'" @click="changeOpentype(2)">
							<image class="vote3Img"
								:src="opentype==2?'@/static/images/mine/vote3_active.png':'@/static/images/mine/vote3.png'"
								mode=""></image>
							<view>統編</view>
						</view>
						<view :class="opentype==3?'flexleft select':'flexleft noSelect'" @click="changeOpentype(3)">
							<image class="vote4Img"
								:src="opentype==3?'@/static/images/mine/vote4_active.png':'@/static/images/mine/vote4.png'"
								mode=""></image>
							<view>捐贈</view>
						</view>
					</view>
					<view class="invoiceInput margin_top flexbetween" v-show="opentype!=0">
						<input class="text_b" type="text" placeholder="請輸入手機載具" v-show="opentype==1"
							:disabled="istijiao" v-model="tbcode">
						<input class="text_b" type="text" placeholder="請輸入統一編碼" v-show="opentype==2" @blur="getTt()"
							:disabled="istijiao" v-model="tbcode">
						<input class="text_b" type="text" placeholder="請輸入捐贈碼" v-show="opentype==3" :disabled="istijiao"
							v-model="tbcode">
						<view class="" v-show="opentype==1 || opentype==3" @click="getCode()">
							<u-icon name="scan" color="#ffb800" size="28"></u-icon>
						</view>
					</view>
					<view class="invoiceInput margin_top" v-show="opentype==2">
						<input class="text_b" type="text" placeholder="請輸入發票抬頭" v-model="tbmc" :disabled="istijiao">
					</view>
				</template>
			</view>
			<view class="flexbetween mainpadding2 ffffff">
				<view class="hong_text titletext bold" style="font-size: 40rpx;">訂金</view>·
				<input type="number"
					style="text-align: right; min-width: 280rpx;max-width: 280rpx;border: 1rpx solid #FF5C00;padding: 20rpx;height: 80rpx;"
					placeholder="請輸入訂金" class="xiaohong titletext radius" v-model="d_money">
			</view>
		</view>
		<view class="posibottom mainpadding" style="bottom: 0;">
			<view class="flexbetween">
				<view class="leftAllprice flexleft mainpadding ffffff">
					<view class="hei_text main_size">尾款：</view>
					<view class="huang_text main_size" v-text="'$'+w_money"></view>
				</view>
				<view class="flexleft">
					<view class="leftsubmit flexcenter mainpadding" v-if="shopcreatehj==0" @click="backhome">
						<view class="xiaobai main_size">待确定</view>
					</view>
					<view class="rightsubmit flexcenter mainpadding" @click="submit">
						<view class="hei_text main_size">提交</view>
					</view>
				</view>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	import HttpRequest from 'uview-ui/libs/luch-request'

	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"),
				list: {
					data: []
				},
				goodTotalPrice: 0, //商品總價
				id: "", //呼叫訂單id
				d_money: '',
				tbcode: "",
				tbmc: "",
				opentype: 0,
				tbcode1: "",
				jzcode: "",
				zjcode: "",
				istijiao: false,
				appkey: "",
				is_call_sdkfp: 0,
				fpopen: false,
				shopcreatehj: 0
			}
		},
		onLoad(options) {
			if (options.shopcreatehj) {
				this.shopcreatehj = options.shopcreatehj
			}
			this.appkey = uni.getStorageSync("appkey")
			this.is_call_sdkfp = uni.getStorageSync("is_call_sdkfp")
			uni.$on("returnCode", data => {
				this.tbcode = data
			})
			this.id = uni.getStorageSync("order_call_id")
		},
		onUnload() {
			uni.$off("returnCode")
		},
		onShow() {
			this.getList()
		},
		computed: {
			s_money() {
				if (this.fpopen) {
					if (this.opentype == 0 || this.opentype == 1 || this.opentype == 2 || this.opentype == 3) {
						// return (this.goodTotalPrice - (Number(this.goodTotalPrice) / 1.05).toFixed(0))
						// return (Number(this.goodTotalPrice) * 0.05).toFixed(0)
						let price = 0
						this.list.data.forEach(item=>{
							price = Number((item.price * 0.05).toFixed(0)) + Number(price)
						})
						return price
					} else {
						return (Number(this.goodTotalPrice) * 0.05).toFixed(0)
					}
				} else {
					return 0
				}
			},
			w_money() {
				return Number(this.totalPrice) + Number(this.s_money) - Number(this.d_money)
			},
			totalPrice() {
				if (this.fpopen) {
					if (this.opentype == 0 || this.opentype == 1 || this.opentype == 2 || this.opentype == 3) {
						// return ((Number(this.goodTotalPrice) / 1.05).toFixed(0))
						return this.goodTotalPrice
					} else {
						return this.goodTotalPrice
					}
				} else {
					return this.goodTotalPrice
				}
			},

		},
		methods: {
			changeOpentype(val){
				this.opentype = val
				if (this.opentype == 0) this.tbcode = ""
				if (this.opentype == 1) this.tbcode = this.zjcode
				if (this.opentype == 2) this.tbcode = this.tbcode1
				if (this.opentype == 3) this.tbcode = this.jzcode
			},
			editGoods(item) {
				uni.navigateTo({
					url: "/pages/callOutOrder/callGoodsdetail?id=" + item.goods_id + "&editid=" + item.id
				})
			},
			getCode() {
				uni.navigateTo({
					url: "/pages/my/getcodetwm"
				})
			},
			getTt() {
				if (this.opentype != 2) {
					return false
				}
				this.http.request("/api/user/getTbmc", "GET", {
					tbcode: this.tbcode
				}).then(res => {
					if (res.code == 1) {
						this.tbmc = res.data.tbinfo.name
					}
				})
			},
			inputenter(e) { //輸入的點數不能大於訂單點數，大於訂單點數的話刪除最後一位
				if (Number(e.target.value) > (Number(this.totalPrice) + Number(this.s_money))) {
					this.d_money = this.d_money.substr(0, this.d_money.length - 1)
				}
			},
			async getList() { //獲取訂單詳情的商品
				let totalPrice = 0
				let res = await this.http.request('/api/Ordercall/callGoodslist', 'POST', {
					id: this.id
				})
				if (res.code == 1) {
					res.data.calllist.forEach(item => {
						item.logoimage = this.imgUrlUser + item.logoimage
					})
					this.list.data = res.data.calllist
					if (this.list.data.length) {
						this.list.data.forEach(item => {
							totalPrice = Number(totalPrice) + Number(item.price)
						})
					}
					this.goodTotalPrice = totalPrice
					if (res.data.call.opentype != -1 && this.appkey!=''&&this.is_call_sdkfp==0) {
						this.fpopen = true
						this.opentype = res.data.call.opentype
						this.tbcode = res.data.call.tbcode
						this.tbmc = res.data.call.tbmc
					} else {
						let fpinfo = res.data.call.user
						if (fpinfo.opentype != 0) {
							this.opentype = fpinfo.opentype
						}
						if (fpinfo.jzcode) {
							this.jzcode = fpinfo.jzcode
							this.tbcode = fpinfo.jzcode
						}
						if (fpinfo.tbcode) {
							this.tbcode = fpinfo.tbcode
							this.tbcode1 = fpinfo.tbcode
						}
						if (fpinfo.zjcode) {
							this.zjcode = fpinfo.zjcode
							this.tbcode = fpinfo.zjcode
						}
					}
				} else if (res.code == 0) {
					this.list.data = []
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			async removeGoods(id) { //刪除商品
				let res = await this.http.request('/api/Ordercall/delorderGoodslist', 'POST', {
					id
				})
				if (res.code == 1) {
					this.http.toast("刪除成功")
					this.getList()

				} else {
					this.http.toast(res.msg)
				}
			},
			checkservice() { //選擇服務
				uni.navigateTo({
					url: "/pages/callOutOrder/xuanzefw?type=1"
				})
			},
			async submit() { //提交
				if (!this.list.data.length) {
					this.http.toast("請先選擇商品")
					return false
				}
				if (this.appkey != "" && this.fpopen) {
					if (this.opentype == 1 && !this.tbcode) {
						this.http.toast("請輸入手機載具")
						return false
					}
					if (this.opentype == 2 && !this.tbcode) {
						this.http.toast("請輸入統一編碼")
						return false
					}
					if (this.opentype == 2 && !this.tbmc) {
						this.http.toast("請輸入發票擡頭")
						return false
					}
					if (this.opentype == 3 && !this.tbcode) {
						this.http.toast("請輸入捐贈碼")
						return false
					}
				}
				if (this.d_money == "") {
					this.http.toast("請輸入訂金後提交")
					return false
				}
				if (Number(this.w_money) < 0) {
					this.http.toast("訂金不能大於總金額")
					return false
				}
				let data = {
					id: this.id,
					count_money: Number(this.totalPrice) + Number(this.s_money),
					d_money: this.d_money,
					w_money: this.w_money,
					s_money: this.s_money,
					goods_money: this.totalPrice,
					opentype: this.appkey ? this.opentype : -1,
					tbcode: this.appkey ? this.tbcode : '',
					tbmc: this.appkey ? this.tbmc : '',
				}
				if (!this.fpopen) {
					data.opentype = -1
					data.tbcode = ""
					data.tbmc = ""
				}
				let res = await this.http.request('/api/Ordercall/callorderadd', 'POST', data)
				if (res.code == 1) {
					this.http.toast("提交成功")
					setTimeout(() => {
						uni.navigateBack({
							delta: 2
						})
					}, 500)
				} else {
					this.http.toast(res.msg)
				}
			},
			backhome() {
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.select {
		width: 154rpx;
		height: 69rpx;
		text-align: center;
		line-height: 69rpx;
		background: rgba(255, 133, 5, 0.1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid #FF8505;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FF8505;
		position: relative;
	}

	.select::after {
		content: '';
		width: 25rpx;
		height: 20rpx;
		position: absolute;
		right: 0;
		bottom: 0;
		background-image: url('/static/images/mine/invoice_select.png');
		background-size: 100% 100%;
	}

	.vote1Img {
		width: 26rpx;
		height: 26rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.vote2Img {
		width: 23.5rpx;
		height: 23.5rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.vote3Img {
		width: 29rpx;
		height: 29rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.vote4Img {
		width: 27rpx;
		height: 27rpx;
		margin-left: 20rpx;
		margin-right: 10rpx;
	}

	.noSelect {
		width: 154rpx;
		height: 69rpx;
		text-align: center;
		line-height: 69rpx;
		background: rgba(102, 102, 102, .1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid rgba(102, 102, 102, .1);
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.invoiceInput {
		width: 100%;
		height: 91rpx;
		background: rgba(196, 196, 196, .1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		display: flex;
		align-items: center;
		padding-left: 20rpx;

		input {
			width: 80%;
		}
	}
</style>